<?php

namespace framework\system\kernel;

use framework;
use framework\system\kernel\CFile;

class View extends CModel {

    protected $tpl = null;
    protected $theme = "Default";
    protected $suffix = "tpl";

    public function __construct() {
        $config = framework::cm()->get("com.config");
        if (is_object($config)) {
            $this->suffix = $config->get("application.view.suffix");
            $this->theme = $config->get("application.view.theme");
        }
    }

    public function tpl($tpl = "Index") {

        $tpl = str_replace(["\\", ".."], '/', $tpl);
        $this->tpl = $tpl;
        $this->tpl = DIR_APPLICATION_VIEW . DS . $this->theme . DS . $this->tpl . "." . $this->suffix;
    }

    public function layout($layout) {
        $layout = DIR_APPLICATION_VIEW . DS . $this->theme . DS . "layout" . DS . $layout . "." . $this->suffix;
        if (CFile::fileExits($layout)) {
            require $layout;
        } else {
            if (_DEBUG) {
                echo "<br/>" . "Notice:Layout file not found :" . $layout;
            }
        }
    }

    public function assign($data = NULL, $value = null) {
        if (empty($data)) {
            return null;
        }
        if (is_string($data) && !empty($value)) {
            $this->$data = $value;
        }
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                if (is_string($key)) {
                    $this->$key = $value;
                }
            }
        }
    }

    public function setTheme($theme = "Default") {
        $this->theme = str_replace(["\\", ".."], '/', $theme);
    }

    public function setSuffix($suffix) {
        $this->suffix = preg_replace("/[^a-zA-Z]/", '', $suffix);
    }

    public function contents() {
        ob_start();
        if (CFile::fileExits($this->tpl)) {
            require $this->tpl;
        } else {
            if (_DEBUG) {
                echo "<br/>" . "Notice:Tpl file not found :" . $this->tpl;
            }
        }


        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }

    protected function render($render) {


        $controller_obj = null;
        $_render = $render;

        $render = explode('/', $render);
        $action = ucfirst(array_pop($render));

        $controller = ucfirst(array_pop($render));

        $path = implode('/', $render);

        $controller_class = 'myapp' . $controller;
        if (CFile::requireFile(DIR_APPLICATION . DS . "controller" . DS . $path . DS . $controller . EXT)) {
            $controller_obj = new $controller_class;
        } elseif (_DEBUG) {
            echo "<br/>" . "Notice:Render file fail :" . $_render;
        }
        /**
         * 
         */
        $action = 'action' . ucwords($action);
        if (is_callable([$controller_obj, $action])) {
            return $controller_obj->$action();
        } elseif (_DEBUG) {
            echo "<br/>" . "Notice:Render action fail :" . $_render;
        }
    }

}
